<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@page import="java.util.*,java.io.*" %>
<html>
<head>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>在此处插入标题</title>
</head>
<body>
<%
	//type="file" method="post" enctype=?
	FileItemFactory temp=new DiskFileItemFactory();//临时目录
	ServletFileUpload up=new ServletFileUpload(temp);//上传对象
	up.setHeaderEncoding("utf-8");
	//表单中的普通元素和文件都在这个list中
	List<FileItem> list=up.parseRequest(request);
	for(FileItem f:list){
		out.print("<p>");
		if(f.isFormField()){
			//不是文件
			out.print(f.getFieldName()+"=="+f.getString("utf-8"));
			//if.....
		}else{ 
			if(f.getSize()==0)
				continue;//没有选择文件
			out.print("文件名:"+f.getName()+",文件域名："+f.getFieldName());
			//改名字：1）防止覆盖 2:防止中文和特殊字符
			String name=f.getName();
			
			//获得后缀
			String prefix=name.substring(name.lastIndexOf('.'));
			String newFileName=UUID.randomUUID()+prefix;
			out.print(" 新名字："+newFileName);
			//保存导数据库，存newFileName
			f.write(new File(application.getRealPath("img"),newFileName));
		}
		out.print("</p>");
	}
 %>
</body>
</html>